/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */
export const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
});
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

import {formatMessage} from './intl';

export const validateAdd = data => {
    const {packingType, length, width, height, volume, netWeight, grossWeight} = data;
    const msgs = [];
    if(!packingType)
        msgs.push(
            formatMessage({
                id: 'utils.packingType',
                defaultMessage: '包装类型'
            })
        );
    if(!length)
        msgs.push(
            formatMessage({
                id: 'utils.length',
                defaultMessage: '长度'
            })
        );
    if(!width)
        msgs.push(
            formatMessage({
                id: 'utils.width',
                defaultMessage: '宽度'
            })
        );
    if(!height)
        msgs.push(
            formatMessage({
                id: 'utils.height',
                defaultMessage: '高度'
            })
        );
    if(!volume)
        msgs.push(
            formatMessage({
                id: 'utils.volume',
                defaultMessage: '体积'
            })
        );
    if(!netWeight)
        msgs.push(
            formatMessage({
                id: 'utils.netWeight',
                defaultMessage: '净重'
            })
        );
    if(!grossWeight)
        msgs.push(
            formatMessage({
                id: 'utils.grossWeight',
                defaultMessage: '毛重'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};


export const validatePack = data => {
    const {dealerId, brandId, packingType, length, width, height, volume, netWeight, grossWeight, details = []} = data;
    const msgs = [];
    if(!brandId)
        msgs.push(
            formatMessage({
                id: 'utils.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!dealerId)
        msgs.push(
            formatMessage({
                id: 'utils.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!packingType)
        msgs.push(
            formatMessage({
                id: 'utils.packingType',
                defaultMessage: '包装类型'
            })
        );
    if(!length)
        msgs.push(
            formatMessage({
                id: 'utils.length',
                defaultMessage: '长度'
            })
        );
    if(!width)
        msgs.push(
            formatMessage({
                id: 'utils.width',
                defaultMessage: '宽度'
            })
        );
    if(!height)
        msgs.push(
            formatMessage({
                id: 'utils.height',
                defaultMessage: '高度'
            })
        );
    if(!volume)
        msgs.push(
            formatMessage({
                id: 'utils.volume',
                defaultMessage: '体积'
            })
        );
    if(!netWeight)
        msgs.push(
            formatMessage({
                id: 'utils.netWeight',
                defaultMessage: '净重'
            })
        );
    if(!grossWeight)
        msgs.push(
            formatMessage({
                id: 'utils.grossWeight',
                defaultMessage: '毛重'
            })
        );
    if(!details.length)
        msgs.push(
            formatMessage({
                id: 'utils.details',
                defaultMessage: '清单'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};

/**
 *  原始数据转化为支持单元格行合并的数据
 *  使用场景 清单映射清单时，主单字段单元格合并
 *  @data 原表格数据
 *  @groupByKey 合并数据的key (主单数据的Id)
 *  @rowSpanKeyList 需要合并的字段 (编号、名称、操作列等)
 */
import {groupBy} from 'lodash';
export const generateRowSpanData = (data, groupByKey, rowSpanKeyList) => {
    const groupByData = groupBy(data, groupByKey);
    const result = [];
    for(const key in groupByData) {
        const array = groupByData[key];
        array.forEach((item, index) => {
            if(index === 0) {
                item.rowSpan = array.length;
                result.push(item);
            } else {
                rowSpanKeyList.forEach(rowSpanKey => {
                    delete item[rowSpanKey];
                });
                result.push(item);
            }
        });
    }
    return result;
};
